package LibraryManagementSystem.book;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/19 22:09
 * 书架
 */
public class Bookcase {
    private static final List<Book> books = new ArrayList<>();
    private static final List<String> allBooksName = new ArrayList<>();

    static {
        books.add(new Book("JAVA程序设计", "liuda066", 68.8, "编程学习"));
        books.add(new Book("Linux从入门到入狱", "Leon", 48.8,"操作系统学习"));
        books.add(new Book("三体", "刘慈欣", 188.8, "小说"));
        books.add(new Book("计算机网络", "谢希仁", 59.99, "计算机基础知识"));

        allBooksName.add("JAVA程序设计");
        allBooksName.add("Linux从入门到入狱");
        allBooksName.add("三体");
        allBooksName.add("计算机网络");
    }

    public void addBook(Book book) {
        books.add(book);
        allBooksName.add(book.getName());
    }

    public boolean isBookContains(String bookName) {
        if (allBooksName.contains(bookName)) {
            return true;
        }
        return false;
    }


    private Book findBookByName(String bookName) {
        if (allBooksName.contains(bookName)) {
            Iterator<Book> iterable = books.iterator();
            while (iterable.hasNext()) {
                Book book = iterable.next();
                if (book.getName().equals(bookName)) {
                    return book;
                }
            }
        } else {
            System.out.println("书籍不存在！");
        }
        return null;
    }

    public void updateBook(String booksName, double price, String type) {
        Book book = findBookByName(booksName);
        book.setPrice(price);
        book.setType(type);
    }



    public boolean deleteBooks(String booksName){
        Book book = findBookByName(booksName);
        if (book == null){
            return false;
        }else {
            books.remove(book);
            allBooksName.remove(booksName);
            return true;
        }
    }

    public void listBooks(){
        Iterator<Book> iterator = books.iterator();
        while (iterator.hasNext()){
            Book book = iterator.next();
            System.out.println(book);
        }
    }

    public boolean findBooks(String booksName){
        Book book = findBookByName(booksName);
        return !(book==null);
    }

    public void borrowBook(String booksName){
        Book book = findBookByName(booksName);
        if (book == null){
            return;
        }
        if (book.isBorrowed()){
            System.err.println(booksName+"已经被别的小伙伴借走啦~");
        }else {
            System.out.println("借书成功啦~");
            book.setBorrowed(true);
        }
    }

    public void returnBook(String booksName){
        Book book = findBookByName(booksName);
        if (book == null) {
            return;
        }
        if(!book.isBorrowed()){
            System.err.println(booksName+"还在图书馆没有借出哦~");
        }else {
            System.out.println("还书成功！");
            book.setBorrowed(false);
        }
    }
}
